import { BaseEntity, Column, Entity, Index, OneToMany, PrimaryGeneratedColumn } from "typeorm";
import { TransferToken } from "./TransferToken";

export enum TransactionStatus {
  Pending,
  Submitted,
  Done,
  Failed
}

@Entity()
export class DepositEther extends BaseEntity {
  @PrimaryGeneratedColumn({ type: 'bigint' })
  id: bigint;

  @Column({ nullable: true })
  txid: string;

  @Column()
  @Index()
  sender: string;

  @Column()
  @Index()
  toAddr: string;

  @Column({ nullable: true })
  nonce: number;

  @Column({ nullable: true })
  gasPrice: string;

  @Column()
  @Index()
  status: TransactionStatus;

  @Column()
  amount: string;

  @Column({ nullable: true, type: "text" })
  note: string;

  @Column({ type: 'timestamp', default: () => "CURRENT_TIMESTAMP" })
  timestamp: string;

  @OneToMany(() => TransferToken, t => t.deposit)
  transfers: TransferToken[];
}

export class SerializedDepositEther {
  id: bigint;
  txid: string;
  sender: string;
  toAddr: string;
  nonce: number;
  gasPrice: string;
  status: TransactionStatus;
  amount: string;
  note: string;
  timestamp: string;
  transfers: bigint[];

  static fromDepositEther(e: DepositEther) {
    const obj = new SerializedDepositEther();
    obj.id = e.id;
    obj.txid = e.txid;
    obj.sender = e.sender;
    obj.toAddr = e.toAddr;
    obj.nonce = e.nonce;
    obj.gasPrice = e.gasPrice;
    obj.status = e.status;
    obj.amount = e.amount;
    obj.note = e.note;
    obj.timestamp = e.timestamp;
    obj.transfers = e.transfers.flatMap(e => e.id);
    return obj;
  }
}
